package com.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.utils.UserContext;
import com.leyou.dto.AddressDTO;
import com.leyou.dto.SaveAddressDto;
import com.leyou.dto.UserDTO;
import com.leyou.entity.Address;
import com.leyou.entity.User;
import com.leyou.mapper.AddressMapper;
import com.leyou.mapper.UserMapper;
import com.leyou.myException.LyException;
import com.leyou.service.AddressService;
import com.leyou.service.UserService;
import com.leyou.utils.RegexUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.leyou.constants.MQConstants.ExchangeConstants.SMS_EXCHANGE_NAME;
import static com.leyou.constants.MQConstants.RoutingKeyConstants.VERIFY_CODE_KEY;

/**
 * @author jshstart
 * @creat 2020-12-22 16:46
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Override
    public List<AddressDTO> queryAllAddress() {
        List<Address> list = query().eq("user_id",UserContext.getUser().getId()).list();
        List<AddressDTO> addressDTOList = AddressDTO.convertEntityList(list);

        return addressDTOList;
    }

    @Override
    public void saveAddress(SaveAddressDto saveAddressDto) {
        String home = saveAddressDto.getHome();
        Address address = new Address();
        //河南省巩义市交通路智慧苑
        String province = home.substring(0, 3);
        System.out.println("province = " + province);
        String city = home.substring(3, 6);
        System.out.println("city = " + city);
        String district = home.substring(6, 9);
        System.out.println("district = " + district);
        String street = home.substring(9);
        System.out.println("street = " + street);
        address.setUserId(UserContext.getUser().getId());
        address.setPostcode(saveAddressDto.getPostcode());
        address.setStreet(street);
        address.setProvince(province);
        address.setPhone(saveAddressDto.getPhone());
        address.setDistrict(district);
        address.setCity(city);
        address.setAddressee(saveAddressDto.getAddressee());
        Address  AddressExist = query().eq("addressee", address.getAddressee()).one();
        System.out.println("AddressExist = " + AddressExist);
        if (AddressExist==null){
            save(address);
        }else {
            address.setId(AddressExist.getId());
            System.out.println(address);

            updateById(address);
        }
    }
}